package com.edu.chongdian;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.tbruyelle.rxpermissions3.RxPermissions;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import io.reactivex.rxjava3.functions.Consumer;

public class SaomaMainActivity extends AppCompatActivity implements QRCodeView.Delegate {
    private ZXingView mZxingView;
    private EditText Text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.saoma_activity_main);
        new RxPermissions(this).request(Manifest.permission.CAMERA)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Throwable {
                        if (aBoolean){
                        }
                    }
                });
        Text=findViewById(R.id.edittext);
        mZxingView = findViewById(R.id.zxing_view);
        mZxingView.setDelegate(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mZxingView.startCamera();// 打开后置摄像头开始预览，但是并未开始识别
        //mZxingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);// 打开前置摄像头开始预览，但是并未开始识别
        // mZxingView.startSpot();// 开始识别
        mZxingView.startSpotAndShowRect();// 显示扫描框，并开始识别
    }


    @Override
    protected void onStop() {
        super.onStop();
        mZxingView.stopCamera();// 关闭摄像头预览，并隐藏扫描框
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Toast.makeText(getBaseContext(),"扫描结果为："+result,Toast.LENGTH_SHORT).show();
        mZxingView.stopSpot();// 停止识别
        Intent intent = new Intent(SaomaMainActivity.this,Saomajieguo_MainActivity.class);
            intent.putExtra("key",result);
        startActivity(intent); // 跳转到相应的页面
        finish();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        Log.d("IS_DARK",String.valueOf(isDark));
    }
    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(getBaseContext(),"ERROR",Toast.LENGTH_SHORT).show();
        mZxingView.stopSpot();// 停止识别
        finish();
    }
}
